(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.deliMessage.replyController', replyController);
	replyController.$inject = [ '$scope', '$timeout', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'deliMessageService', '$filter', 'modalService' ];
	function replyController($scope, $timeout, Constants, layoutConfig, validatorConfig, gridService, authService, deliMessageService, $filter, modalService) {
		var ctrl = this, deliMessageInfo = $scope.$stateParams.params;
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.deliMessage = {};

		ctrl.init = function() {
			deliMessageService.initGeneral($scope, ctrl, true).then(function() {
				authService.reply(deliMessageInfo).then(function(response) {
					var initDeliMessage = response.data || deliMessageInfo;
					initDeliMessage.replyFlag = 'Y';
					ctrl.layoutConfig = layoutConfig;
					$timeout(function() {
						ctrl.deliMessage = initDeliMessage;
					});
				});
			});
		};
		
		ctrl.send = function() {
			authService.send(ctrl.deliMessage).then(sendSuccessful, sendFailed);
		};
		
		function sendSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.send' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.deliMessage.list');
					}
				},
				'redirections' : [{
					'name' : 'title.infobar.delimessage.list',
					'state' : 'cda.deliMessage.list',
					'params' : {}
				}]
			});
		}

		function sendFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);